package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Qdkb;
import com.ruoyi.system.domain.bo.SlmxBo;
import com.ruoyi.system.domain.vo.SlmxVo;
import com.ruoyi.system.service.IQdkbService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Slmx;
import com.ruoyi.system.service.ISlmxService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收入明细Controller
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/system/slmx")
public class SlmxController extends BaseController
{
    @Autowired
    private ISlmxService slmxService;
    @Autowired
    private IQdkbService qdkbService;

    /**
     * 查询收入明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:slmx:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlmxVo slmxVo)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains3OR4 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==3L || v.getRoleId()==4L);
        boolean contains6 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==6L);

       if(contains3OR4){
            slmxVo.setTlId(getUserId());
        }
        if(contains6){
            slmxVo.setPmId(getUserId());
        }
        startPage();
        List<SlmxBo> list = slmxService.selectSlmxList(slmxVo);
        return getDataTable(list);
    }


    /**
     * 导出收入明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:slmx:export')")
    @Log(title = "收入明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlmxVo slmxVo)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains3OR4 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==3L || v.getRoleId()==4L);
        boolean contains6 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==6L);

        if(contains3OR4){
            slmxVo.setTlId(getUserId());
        }
        if(contains6){
            slmxVo.setPmId(getUserId());
        }
        List<SlmxBo> list = slmxService.selectSlmxList(slmxVo);
        ExcelUtil<SlmxBo> util = new ExcelUtil<SlmxBo>(SlmxBo.class);
        util.exportExcel(response, list, "收入明细数据");
    }

    /**
     * 获取收入明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:slmx:query')")
    @GetMapping(value = "/{slmxId}")
    public AjaxResult getInfo(@PathVariable("slmxId") Long slmxId)
    {
        return success(slmxService.selectSlmxBySlmxId(slmxId));
    }

    /**
     * 新增收入明细
     */
    @PreAuthorize("@ss.hasPermi('system:slmx:add')")
    @Log(title = "收入明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlmxVo slmxVo)
    {
        LambdaQueryWrapper<Qdkb> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Qdkb::getRebatePrice,Qdkb::getRefundPeriod,Qdkb::getRebateType);
        lambdaQueryWrapper.eq(Qdkb::getQdkbId,slmxVo.getQdkbId());
        Qdkb dbQdkb = qdkbService.getOne(lambdaQueryWrapper);

        LambdaQueryWrapper<Slmx> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.select(Slmx::getSlmxId);
        lambdaQueryWrapper1.eq(Slmx::getYgId,slmxVo.getYgId());
        long count = slmxService.count(lambdaQueryWrapper1);
        if(count>=dbQdkb.getRefundPeriod().intValue()){
            throw new RuntimeException("工单周期已全部生成完毕，请查看收入明细");
        }
        Slmx slmx=new Slmx();
        slmx.setYgId(slmxVo.getYgId());
        if(dbQdkb.getRebateType().equals("一次性返费")){
            slmx.setMoney(dbQdkb.getRebatePrice());
        }
        return toAjax(slmxService.insertSlmx(slmx));
    }

    /**
     * 修改收入明细
     */
    @PreAuthorize("@ss.hasPermi('system:slmx:edit')")
    @Log(title = "收入明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Slmx slmx)

    {
        LambdaQueryWrapper<Slmx> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Slmx::getSlmxId,Slmx::getYgId);
        lambdaQueryWrapper.eq(Slmx::getSlmxId,slmx.getSlmxId());
        lambdaQueryWrapper.eq(Slmx::getBackStatus,"1");
        Slmx dbSlmx = slmxService.getOne(lambdaQueryWrapper);
        if(dbSlmx==null){
            throw new RuntimeException("该数据状态已有最新");
        }

        return toAjax(slmxService.updateSlmx(slmx,dbSlmx.getYgId()));
    }

    @PreAuthorize("@ss.hasPermi('system:slmx:edit')")
    @Log(title = "收入明细", businessType = BusinessType.UPDATE)
    @PutMapping("/repeatEdit")
    public AjaxResult repeatEdit(@RequestBody Slmx slmx)
    {
        LambdaQueryWrapper<Slmx> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Slmx::getSlmxId,Slmx::getYgId);
        lambdaQueryWrapper.eq(Slmx::getSlmxId,slmx.getSlmxId());
        lambdaQueryWrapper.eq(Slmx::getBackStatus,"3");
        Slmx dbSlmx = slmxService.getOne(lambdaQueryWrapper);
        if(dbSlmx==null){
            throw new RuntimeException("该数据状态已有最新");
        }
        return toAjax(slmxService.updateSlmx(slmx,dbSlmx.getYgId()));
    }

    /**
     * 删除收入明细
     */
    @PreAuthorize("@ss.hasPermi('system:slmx:remove')")
    @Log(title = "收入明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{slmxIds}")
    public AjaxResult remove(@PathVariable Long[] slmxIds)
    {
        return toAjax(slmxService.deleteSlmxBySlmxIds(slmxIds));
    }
}
